var express = require('express');
var router = express.Router();
var connection = require('../db/sql.js');
connection.connect()
// var jwt_decode = require('jwt-decode');
// var jwt = require('jsonwebtoken');
/* GET home page. */
router.get('/', function(req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});

router.all('*', function(req, res, next) {
	res.header('Access-Control-Allow-Origin', '*');
	//Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	res.header('Access-Control-Allow-Methods', '*');
	res.header('Content-Type', 'application/json;charset=utf-8');
	next();
});


/* 商品搜索列表页 */
router.get('/api/goods/search', function(req, res, next) {
	// // asc 升序  desc 降序
	// orderName 字段key
	let [goodsName, orderName] = Object.keys(req.query);
	// 查询商品 输入的值
	let name = req.query.name;
	//商品分类key值
	let order = req.query[orderName];
	connection.query("select * from goods_search where name like '%" + name + "%' order by " + orderName +
		" " + order + "",
		function(error, results, fields) {
			if (error) throw error
			res.send({
				code: "200",
				data: results
			})
		})
});

/* 产品详情 */
router.get('/api/goods/id', function(req, res, next) {
	let id = req.query.id
	console.log(typeof id);
	connection.query("select * from goods_search where id=" + id + "",
		function(error, results) {
			if (error) throw error
			res.send({
				code: "200",
				data: results
			})
		})
});


/* 首页分类topBar */
router.get('/api/index/top_bar', function(req, res, next) {
	connection.query("select * from top_bar",
		function(error, results, fields) {
			if (error) throw error
			res.send({
				code: "200",
				data: results
			})
		})
});

/* 首页轮播 */
router.get('/api/index/swipers', function(req, res, next) {
	connection.query("select * from swiper_list",
		function(error, results, fields) {
			if (error) throw error
			res.send({
				code: "200",
				data: results,
			})
		})
});

// 商品列表
router.get('/api/index/commodity_list', function(req, res, next) {
	const page = req.query.page || 1;
	const pageSize = req.query.pageSize;
	const offset = (page - 1) * pageSize;

	connection.query(`select * from goods_search limit ${pageSize} offset ${offset}`,
		function(error, results, fields) {
			if (error) throw error
			connection.query(`select count(*) as total from goods_search`, function(err, ountResult) {
				if (err) throw err
				let total = ountResult[0].total
				let totalPage = Math.ceil(total / pageSize);
				res.json({
					data: {
						results,
						totalCount: total
					}
				});
			})
		})
});

router.get('/api/index_list/data', function(req, res, next) {
	res.send({
		"code": 200,
		"data": {
			swiperList: [{
				"imagUrl": "https://picsum.photos/id/237/200/300"
			}, {
				"imagUrl": "https://picsum.photos/seed/picsum/200/300"
			}, {
				"imagUrl": "https://picsum.photos/200/300?grayscale"
			}],
			recommendList: [{
				bigUrl: "../../static/1.jpg",
				data: [{
					smallImg: "../../static/1.jpg"
				}, {
					smallImg: "https://picsum.photos/seed/picsum/200/300"
				}, {
					smallImg: "../../static/1.jpg"
				}, ]
			}, {
				bigUrl: "../../static/1.jpg",
				data: [{
					smallImg: "../../static/1.jpg"
				}, {
					smallImg: "https://picsum.photos/seed/picsum/200/300"
				}, {
					smallImg: "../../static/1.jpg"
				}, ]
			}, ],
			commodityList: [{
				id: '1',
				img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
				price: 1000,
				discount: 5.3,
				name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
			}, {
				id: '2',
				img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品大蚂蚁哟'
			}, {
				id: '3',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品'
			}]
		}
	})
});
// 首页分类数据
router.get('/api/index_list/1/data/1', function(req, res, next) {
	res.send({
		"code": 200,
		"data": [{
			type: 'bannerList',
			url: 'https://picsum.photos/seed/picsum/200/300'
		}, {
			type: 'iconList',
			data: [{
				text: '运动户外',
				url: 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'
			}, {
				text: '运动户外',
				url: 'http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg'
			}, {
				text: '运动户外',
				url: 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'
			}, {
				text: '运动户外',
				url: 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'
			}, ]
		}, {
			type: 'hotList',
			data: [{
				id: '1',
				img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
				price: 1000,
				discount: 5.3,
				name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
			}, {
				id: '2',
				img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品大蚂蚁哟'
			}, {
				id: '3',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品'
			}, {
				id: '4',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品打色'
			}, {
				id: '5',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 2300,
				discount: 1.3,
				name: '新款商品'
			}, {
				id: '6',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 4356,
				discount: 2.3,
				name: '新款商品'
			}]
		}, {
			type: 'shopList',
			data: [{
				bigUrl: "https://picsum.photos/200/300/?blur",
				data: [{
					id: '1',
					img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
					price: 1000,
					discount: 5.3,
					name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
				}, {
					id: '2',
					img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品大蚂蚁哟'
				}, {
					id: '3',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品'
				}]
			}, {
				bigUrl: "https://picsum.photos/200/300/?blur",
				data: [{
					id: '1',
					img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
					price: 1000,
					discount: 5.3,
					name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
				}, {
					id: '2',
					img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品大蚂蚁哟'
				}, {
					id: '3',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品'
				}, {
					id: '4',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品打色'
				}, {
					id: '5',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 2300,
					discount: 1.3,
					name: '新款商品'
				}, {
					id: '6',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 4356,
					discount: 2.3,
					name: '新款商品'
				}]
			}, ]
		}, {
			type: "commodityList",
			data: [{
				id: '1',
				img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
				price: 1000,
				discount: 5.3,
				name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
			}, {
				id: '2',
				img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品大蚂蚁哟'
			}, {
				id: '3',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品'
			}]
		}]
	});
});
router.get('/api/index_list/2/data/1', function(req, res, next) {
	res.send({
		"code": 200,
		"data": [{
			type: 'bannerList',
			url: 'https://picsum.photos/seed/picsum/200/300'
		}, {
			type: 'iconList',
			data: [{
				text: '服饰内衣',
				url: 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'
			}, {
				text: '服饰内衣',
				url: 'http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg'
			}, {
				text: '服饰内衣',
				url: 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'
			}, , {
				text: '服饰内衣',
				url: 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'
			}, ]
		}, {
			type: 'hotList',
			data: [{
				id: '1',
				img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
				price: 1000,
				discount: 5.3,
				name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
			}, {
				id: '2',
				img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品大蚂蚁哟'
			}, {
				id: '3',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品'
			}]
		}, {
			type: 'shopList',
			data: [{
				bigUrl: "https://picsum.photos/200/300/?blur",
				data: [{
					id: '1',
					img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
					price: 1000,
					discount: 5.3,
					name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
				}, {
					id: '2',
					img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品大蚂蚁哟'
				}, {
					id: '3',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品'
				}]
			}, {
				bigUrl: "https://picsum.photos/200/300/?blur",
				data: [{
					id: '1',
					img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
					price: 1000,
					discount: 5.3,
					name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
				}, {
					id: '2',
					img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品大蚂蚁哟'
				}, {
					id: '3',
					img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
					price: 10002,
					discount: 5.3,
					name: '新款商品'
				}]
			}, ]
		}, {
			type: "commodityList",
			data: [{
				id: '1',
				img: 'http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg"',
				price: 1000,
				discount: 5.3,
				name: '新款商品新款商品新款商品新款商品新款商品新款商品新款商品'
			}, {
				id: '2',
				img: 'http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品大蚂蚁哟'
			}, {
				id: '3',
				img: 'http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg',
				price: 10002,
				discount: 5.3,
				name: '新款商品'
			}]
		}]
	});
});

module.exports = router;
